import { HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common';
import { PrismaService } from '../../middleware/prisma/prisma.service';
import { CreatePlateDto } from './dto/create-plate.dto';
import { UpdatePlateDto } from './dto/update-plate.dto';

@Injectable()
export class PlateService {
  @Inject(PrismaService)
  private prismaService: PrismaService;
  async create(createPlateDto: CreatePlateDto) {
    await this.prismaService.plate.create({
      data: createPlateDto,
      select: {
        id: true,
        plateName: true,
        description: true,
        iconURL: true,
        pathURL: true,
        routingTable: true,
        status: true,
        createTime: true,
        updateTime: true,
      },
    });
    return '新增成功';
  }

  async list(status: string, pageNum: number, pageSize: number) {
    if (!pageNum) {
      pageNum = 1;
    }
    if (!pageSize) {
      pageSize = 10;
    }
    const plate = await this.prismaService.plate.findMany({
      skip: (Number(pageNum) - 1) * Number(pageSize),
      take: Number(pageSize),
      where: {
        isDetele: '1',
        status: status,
      },
      select: {
        id: true,
        plateName: true,
        description: true,
        iconURL: true,
        pathURL: true,
        routingTable: true,
        status: true,
        createTime: true,
        updateTime: true,
      },
    });

    const result = {
      list: plate,
      pageNum: Number(pageNum),
      pageSize: Number(pageSize),
      total: plate.length,
    };
    return result;
  }

  async update(updatePlateDto: UpdatePlateDto) {
    try {
      await this.prismaService.plate.update({
        where: {
          isDetele: '1',
          id: updatePlateDto.id,
        },
        data: {
          plateName: updatePlateDto.plateName,
          description: updatePlateDto.description,
          iconURL: updatePlateDto.iconURL,
          pathURL: updatePlateDto.pathURL,
          routingTable: updatePlateDto.routingTable,
        },
        select: {
          id: true,
          plateName: true,
          description: true,
          iconURL: true,
          pathURL: true,
          routingTable: true,
          status: true,
          createTime: true,
          updateTime: true,
        },
      });
    } catch (error) {
      throw new HttpException('该消息不存在，修改失败', HttpStatus.BAD_REQUEST);
    }

    return '修改成功';
  }

  async romove(id: number) {
    // 数据库删除
    // return this.prismaService.inform.delete({
    //   where: {
    //     id,
    //   },
    // });
    // 业务逻辑删除（推荐）
    try {
      await this.prismaService.plate.update({
        where: {
          id: Number(id),
        },
        data: {
          isDetele: '0',
        },
        select: {
          id: true,
          plateName: true,
          description: true,
          iconURL: true,
          pathURL: true,
          routingTable: true,
          status: true,
          createTime: true,
          updateTime: true,
        },
      });
    } catch (error) {
      throw new HttpException('该消息不存在，删除失败', HttpStatus.BAD_REQUEST);
    }
    return '删除成功';
  }
}
